﻿#/usr/bin/python

import sys
import serial
import serial.tools.list_ports
from PyQt4.QtCore import *
from PyQt4.QtGui import *

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8")) 


class MainWindow(QMainWindow):
    
    def __init__(self):
        super(MainWindow,self).__init__()
        self.initWindow()
        self.portSetting = {}
        self.portSettingBuf = {}
        
    def initWindow(self):
        self.resize(250*4,150*4)
        self.setWindowTitle('PyQtCom')
        self.setWindowIcon(QIcon('icons/chrome_bw.png'))
        self.statusBar().showMessage(self.tr("请配置您的串口"))
        
        centralWidget = QWidget()
        
        centralLayout = QVBoxLayout()
        
        textEdit = QTextBrowser()
        lineEdit = QLineEdit()
        
        send = QPushButton(self.tr("发送"))
        send.resize(30,25)
        
        actionLayout = QHBoxLayout()
        actionLayout.addWidget(send)
        
        centralLayout.addWidget(textEdit)
        centralLayout.addLayout(actionLayout)
        centralLayout.addWidget(lineEdit)
        
        centralWidget.setLayout(centralLayout)
        
        self.setCentralWidget(centralWidget)
        
        menubar = self.menuBar()
        
        open = QAction(QIcon('icons/open.png'),'&Open',self)
        open.setShortcut('Ctrl+O')
        open.setStatusTip(self.tr("打开串口"))
        open.setToolTip(self.tr("打开串口"))
        open.connect(open,SIGNAL('triggered()'),self.open_port)
        
        exit = QAction(QIcon('icons/exit.png'),'&Exit',self)
        exit.setShortcut('Ctrl+Q')
        exit.setStatusTip(self.tr('退出程序'))
        exit.setToolTip(self.tr('退出程序'))
        exit.connect(exit,SIGNAL('triggered()'),qApp,SLOT('quit()'))
        
        
        file = menubar.addMenu('&File')
        
        file.addAction(open)
        file.addAction(exit)
        
        toolbar = self.addToolBar('Open')
        toolbar.addAction(open)
        toolbar.addAction(exit)
        
        
    def open_port(self):
        dialog = QDialog()
        dialog.setWindowTitle("Port Setting")
        dialog.setWindowIcon(QIcon('icons/serial_port.png'))
        
        vbox = QVBoxLayout()
        
        portLabel = QLabel(self.tr("端口"))
        portSelect = QComboBox()
        portLabel.setBuddy(portSelect)
        portSelect.addItems(list_all_ports())
        
        baudSet = QLabel(self.tr("波特率"))
        baudSelect = QComboBox()
        baudSet.setBuddy(baudSelect)
        baudSelect.addItems(['115200','9600','19200','57600','600','1200','2400','4800','14400','28800','38400','230400','460800'])
        
        paritySet = QLabel(self.tr("校验"))
        paritySelect = QComboBox()
        paritySet.setBuddy(paritySelect)
        paritySelect.addItem(self.tr("无"))
        paritySelect.addItem(self.tr("奇校验"))
        paritySelect.addItem(self.tr("偶校验"))
        
        stopbitSet = QLabel(self.tr("停止位"))
        stopbitSelect = QComboBox()
        stopbitSet.setBuddy(stopbitSelect)
        stopbitSelect.addItems(['1','2'])
        
        okButton = QPushButton(self.tr("确定"))
        cancleButton = QPushButton(self.tr("取消"))
        hbox1 = QHBoxLayout()
        hbox2 = QHBoxLayout()
        
        hbox1.addWidget(portLabel)
        hbox1.addWidget(portSelect)
        hbox1.addWidget(baudSet)
        hbox1.addWidget(baudSelect)
        hbox1.addWidget(paritySet)
        hbox1.addWidget(paritySelect)
        hbox1.addWidget(stopbitSet)
        hbox1.addWidget(stopbitSelect)
        
        hbox2.addWidget(okButton)
        hbox2.addWidget(cancleButton)
        
        vbox.addLayout(hbox1)
        vbox.addLayout(hbox2)
        
        dialog.setLayout(vbox)
        
        dialog.exec_()

    def get_setting(self):
        self.portSetting['stopbit'] = self.portSettingBuf['stopbit']
        self.portSetting['baudrate'] = self.portSettingBuf['baudrate']
        self.portSetting['parity'] = self.portSettingBuf['parity']
        
    
def list_all_ports():
    ports = list(serial.tools.list_ports.comports())
    COMS = []
    for item in ports:
        COMS.append(item[0])
    return COMS
    
    
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
